package com.egoo.ticket.server.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.dao.EwsDepartmentDao;
import com.egoo.ticket.server.dao.EwsRoleDao;
import com.egoo.ticket.server.dao.EwsUserDao;
import com.egoo.ticket.server.dao.WorkOrderTypeNodeDao;
import com.egoo.ticket.server.pojo.entity.WorkOrder;
import com.egoo.ticket.server.pojo.vo.InteractiveDataVo;
import com.egoo.ticket.server.pojo.vo.WorkOrderTypeNodeVo;
import com.egoo.ticket.server.service.UserService;
import com.egoo.ticket.server.service.WorkOrderTypeNodeService;
import com.egoo.ticket.server.utils.common.CommonUtil;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class WorkOrderTypeNodeServiceImpl implements WorkOrderTypeNodeService {

	@Autowired
	WorkOrderTypeNodeDao workOrderTypeNodeDao;
	@Autowired
	UserService userService;
	@Autowired
	EwsDepartmentDao ewsDepartmentDao;
	@Autowired
	EwsRoleDao ewsRoleDao;
	@Autowired
	EwsUserDao ewsUserDao;
	@Autowired
	CommonUtil commonUtil;

	@Override
	public Integer create(WorkOrderTypeNodeVo workOrderTypeNodeVo) {
		return workOrderTypeNodeDao.create(workOrderTypeNodeVo);
	}

	/**
	 * TODO 根据工单类型Id查询节点集合
	 * @author: victor_tang
	 * @createtime: 2020/4/9 18:55
	 * @param workOrderTypeId 1
	 * @param tenantId 2
	 * @return: java.utils.List<com.egoo.ews.service.vo.InteractiveData>
	 */
	@Override
	public List<InteractiveDataVo> selectByWorkOrderTypeId(Long workOrderTypeId, String tenantId) {
		Map<String, Object> searchMap = new HashMap<>(4);
		searchMap.put("tenantId",tenantId);
		searchMap.put("workOrderTypeId",workOrderTypeId);
		return workOrderTypeNodeDao.selectInteractiveListByCondition(searchMap);
	}

	/**
	 * TODO 根据工单类型查询所有的节点的表单id
	 * @param workOrderTypeId
	 * @return
	 */
	@Override
	public List<WorkOrderTypeNodeVo> selectFormkeyByWorkOrderTypeId(Long workOrderTypeId,String tenantId) {
		Map<String, Object> searchMap = new HashMap<>(4);
		searchMap.put("workOrderTypeId",workOrderTypeId);
		searchMap.put("tenantId",tenantId);
		return workOrderTypeNodeDao.selectNodeListByCondition(searchMap);
	}
	/**
	 * TODO 根据工单类型节点id查询节点信息
	 * @author: victor_tang
	 * @createtime: 2020/3/19 16:39
	 * @param workOrderTypeNodeId 1
	 * @return: com.egoo.ews.service.vo.WorkOrderTypeNodeVo
	 */
	@Override
	public WorkOrderTypeNodeVo getNodeInfoByWorkOrderTypeId(Long workOrderTypeNodeId,String tenantId) {
		return workOrderTypeNodeDao.selectNodeInfoByWorkOrderNodeId(workOrderTypeNodeId,tenantId);
	}

	/**
	 *TODO  根据工单类型Id和名字模糊搜索工单节点
	 * @param workOrderTypeId
	 * @param nodeName
	 * @return
	 */
	@Override
	public List<InteractiveDataVo> selectByWorkOrderTypeIdAndNodeName(Long workOrderTypeId, String nodeName, String tenantId) {
		Map<String, Object> searchMap = new HashMap<>(8);
		searchMap.put("tenantId",tenantId);
		searchMap.put("workOrderTypeId",workOrderTypeId);
		searchMap.put("nodeName",nodeName);
		return workOrderTypeNodeDao.selectInteractiveListByCondition(searchMap);
	}

	/**
	 * TODO 根据节点id获取节点名字
	 * @param workOrderNodeId
	 * @return
	 */
	@Override
	public InteractiveDataVo selectByWorkOrderNodeId(Long workOrderNodeId) {
		return workOrderTypeNodeDao.selectByWorkOrderNodeId(workOrderNodeId);
	}
	/**
	 * TODO 根据节点id获取节点处理人信息
	 * @author: victor_tang
	 * @createtime: 2020/3/19 18:49
	 * @param workOrderTypeNodeId 1
	 * @param tenantId 2
	 * @param userId
	 * @return: com.egoo.ews.service.vo.WorkOrderTypeNodeVo
	 */
	@Override
	public JSONObject getNodeHandleUserInfoByWorkOrderTypeId(Long workOrderTypeNodeId,
															 String tenantId, String userId) {
		WorkOrderTypeNodeVo workOrderTypeNodeVo = workOrderTypeNodeDao.selectNodeInfoByWorkOrderNodeId(workOrderTypeNodeId, tenantId);

		if (workOrderTypeNodeVo==null||workOrderTypeNodeVo.getContent()==null
				||workOrderTypeNodeVo.getContent().getJSONArray("assignee")==null
				||workOrderTypeNodeVo.getContent().getJSONArray("assignee").size()==0
		) {
			log.info("工单类型节点为空或者节点处理人信息为空！");
			return null;
		}
		JSONObject content = workOrderTypeNodeVo.getContent();
		String type = content.getString("assigneetype");
		List<String> typeIds = null;
		if (ConstantCode.UserRoleDeptType.DEPT.equals(type)) {
			JSONArray assignee = content.getJSONArray("assignee");
			typeIds = commonUtil.splitDeptCode(assignee.toJSONString());
		}else{
			typeIds = content.getObject("assignee", List.class);
		}
		Map<String, Object> searchMap = new HashMap<>(8);
		searchMap.put("tenantId",tenantId);

		List<InteractiveDataVo> interactiveData = null;
		if (ConstantCode.UserRoleDeptType.DEPT.equals(type)) {
			searchMap.put("deptIds",typeIds);
			interactiveData = ewsDepartmentDao.selectInteractiveListByCondition(searchMap);
		}
		else if (ConstantCode.UserRoleDeptType.ROLE.equals(type)) {
			searchMap.put("roleIds",typeIds);
			interactiveData = ewsRoleDao.selectRolesInteractiveByCondition(searchMap);
		}
		else if (ConstantCode.UserRoleDeptType.USER.equals(type)) {
			WorkOrder workOrder = new WorkOrder();
			workOrder.setCreateUserId(userId);
			CommonUtil.handleCurrentCreator(typeIds,workOrder);
			searchMap.put("userIds",typeIds);
			interactiveData = ewsUserDao.selectInteractiveUsers(searchMap);
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("assigneetype",type);
		jsonObject.put("assignee",interactiveData);
		return jsonObject;
	}

	@Override
	public DbResponse getTemplateFromOfStartNode(Long workOrderTypeId) {
		log.info("传入的类型id为："+workOrderTypeId);
		WorkOrderTypeNodeVo workOrderTypeNodeVo = workOrderTypeNodeDao.selectStartNodeByWorkOrderTypeId(workOrderTypeId);
		if (workOrderTypeNodeVo==null) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					"未找到工单类型初始节点信息！");
		}
		if (!workOrderTypeNodeVo.getFormKeySign().equals(ConstantCode.WorkOrderNodeInfo.FORM_KEY_SIGN_MORE)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					"该工单类型初始节点信息模板配置类型不是为多模板！");
		}
		JSONArray classifyList = workOrderTypeNodeVo.getContent().getJSONArray("classifyList");
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,"获取成功！",
				classifyList);
	}
}